const Sequelize = require('sequelize')
const seq = require('./seq')

// 创建用户模型
const User = seq.define('user', {
  // id 自动创建， 并设置为主键，自增
  userName: {
    type: Sequelize.STRING,//varchar(255)
    allowNull: false
  },
  password: {
    type: Sequelize.STRING,
    allowNull: false
  },
  nickName: {
    type: Sequelize.STRING,
    comment:'昵称'// 备注
  }
  // 自动创建： createAt 和updateAt
})

// 创建Blog模型
const Blog = seq.define('blog', {
  title: {
    type: Sequelize.STRING,
    allowNull: false
  },
  content: {
    type: Sequelize.TEXT,
    allowNull:false
  },
  userId: {
    type: Sequelize.INTEGER,
    allowNull: false
  }
})

// 外键关联方式一
Blog.belongsTo(User, {
  // 创建外键 Blog.userId ==> User.id
  foreignKey:'userId'
})
// 外键关联方式二
User.hasMany(Blog,{
  foreignKey:'userId'
})

module.exports = {
  User,
  Blog
}